/*
 * Copyright (c) 田梓萱 2021-2022.
 * All Rights Reserved.
 * All codes are protected by China's regulations on the protection of computer software, and infringement must be investigated.
 * 版权所有 (c) 田梓萱 2021-2022.
 * 所有代码均受中国《计算机软件保护条例》保护，本项目已获得软著.
 */

package com.xcl.calculator.slice.QinQiCal;

import com.xcl.calculator.XLog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * The type Sq listm.
 *
 * @author Xcl
 * @date 2022 /1/3
 * @package com.xcl.calculator.slice.QinQiCal
 */
public class SqListm {
    /**
     * The constant label.
     */
    private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00234, "SqListm");
    /**
     * The Data.
     */
    private String[] data = null;
    /**
     * The Length.
     */
    private int length = 0;
    /**
     * The Size.
     */
    private int size = 0;

    /**
     * Instantiates a new Sq listm.
     *
     * @param n the n
     */
    SqListm(int n) {
        try {
            this.size = n;
            this.data = new String[n];
            for (int i = 0; i < this.size; i++) this.data[i] = "";
            this.add("");
            this.add("");
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Instantiates a new Sq listm.
     *
     * @param temp the temp
     */
    SqListm(SqListm temp) {
        try {
            this.data = new String[temp.size];
            for (int i = 0; i < temp.length(); i++) this.data[i] = temp.getItem(i);
            this.length = temp.length();
            this.size = temp.size;
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Is exist boolean.
     *
     * @param item the item
     * @return the boolean
     */
    private boolean isExist(String item) {
        for (int i = 1; i < this.length; i++) if (this.data[i].equals(item)) return true;
        return false;
    }

    /**
     * Is exist boolean.
     *
     * @param position the position
     * @param item     the item
     * @return the boolean
     */
    private boolean isExist(int position, String item) {
        for (int i = position + 1; i < this.length; i++) if (this.data[i].equals(item)) return true;
        return false;
    }

    /**
     * Add.
     *
     * @param item the item
     */
    void add(String item) {
        try {
            if (!this.isExist(item)) this.data[this.length++] = item;
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Find data int.
     *
     * @param item the item
     * @return the int
     */
    public int findData(String item) {
        for (int i = 0; i < this.length; i++) if (this.data[i].equals(item)) return i;
        return -1;
    }

    /**
     * Length int.
     *
     * @return the int
     */
    public int length() {
        return this.length;
    }

    /**
     * Change value.
     *
     * @param position the position
     * @param item     the item
     */
    void changeValue(int position, String item) {
        try {
            if (!this.isExist(position, item)) this.data[position] = item;
            else this.deleteItem(position);
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Delete item.
     *
     * @param position the position
     */
    private void deleteItem(int position) {
        try {
            for (int i = position; i < this.length - 1; i++) this.data[i] = this.data[i + 1];
            this.data[this.length - 1] = "";
            this.length--;
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Clear.
     */
    void clear() {
        try {
            for (int i = 0; i < this.length; i++) this.data[i] = null;
            this.length = 0;
            // 加两个空元素 第一个存放原始的用户输入，第二个存放初始的
            this.add("");
            this.add("");
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Gets item.
     *
     * @param position the position
     * @return the item
     */
    String getItem(int position) {
        if (position < this.length) return this.data[position];
        else return "";
    }

    /**
     * All item append.
     *
     * @param tail the tail
     */
    void allItemAppend(String tail) {
        try {
            for (int i = 0; i < this.length; i++)
                if (this.data[i].length() == 0) this.data[i] = tail;
                else this.data[i] = this.data[i] + "," + tail;
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }

    /**
     * Combine.
     *
     * @param temp the temp
     */
    void combine(SqListm temp) {
        try {
            for (int i = 1; i < temp.length(); i++) this.add(temp.getItem(i));
        } catch (Exception e) {
            XLog.error(label, e.getMessage());
        }
    }
}
